<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class FinanceController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
	public function index(){
		$order_no = I('name')?I('name'):null;
		$type = I('type')?I('type'):null;
		$id = I('id');
		if(!empty($order_no)){
			$where['nickname']=array('like','%'.$order_no.'%');
		}
		if(!empty($type)){
			$where['f.type']=$type;
		}
		if($id){
			$where['open_id'] = $id;
		}
		$count=M('Finance f')->field('f.*,m.userPhone,m.nickname,m.type as mtype')->join("yang_member m ON m.openid = f.open_id")->where($where)->order('addtime desc')->count();
		$Page=new Page($count,10);
		setPageParameter($Page, array('type'=>$type));
		$show       = $Page->show();
		$list =  M('Finance f')->field('f.*,m.userPhone,m.nickname,m.type as mtype')->join("yang_member m ON m.openid = f.open_id")->where($where)->order('addtime desc')
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
	foreach($list as $k=>$v){
	        if($list[$k]['sz_type'] == 1){
	            $list[$k]['sz_type'] = "收入";
	        }
	        if($list[$k]['sz_type'] == 2){
	            $list[$k]['sz_type'] = "支出";
	        }
	         if($list[$k]['type'] == 2){
	           $list[$k]['type'] = "积分";
	        }
	        if($list[$k]['type'] == 1){
	            $list[$k]['type'] = "余额";
	        } 
	    }
		$this->assign('name',$order_no);
		$this->assign('type',$type);
		$this->assign('list',$list);
		$this->assign('id',$id);
		$this->assign('page',$show);
		$this->display();
	}
	public function detail(){
        $order_id = I('order_id');
        $is_tui  = I('is_tui');
        $list = M('Orders o')
                ->field('o.*,o.add_time as oadd_time,m.*')
                ->join('yang_member m ON m.openid=o.open_id')
                ->where('o.order_id ='.$order_id)
                ->find();
        $goods = M('Order_goods og')
                ->field("og.*,ga.attr_val,g.*")
                ->join('yang_goods g ON og.goods_id=g.goods_id')
                ->join('yang_goods_attr ga ON og.attr_id=ga.goods_attr_id')
                ->where('og.order_id ='.$order_id)
                ->select();
        if($is_tui){
            $list = M('Orders o')
            ->field('o.*,o.add_time as oadd_time,m.*')
            ->join('yang_member m ON m.openid=o.open_id')
            ->join('yang_order_tui ot ON ot.order_id=o.order_id')
            ->where(array('o.order_id'=>$order_id,'order_status' =>4))
            ->find();
            
            $goods = M('Order_goods og')
            ->field("og.*,ga.attr_val,g.*")
            ->join('yang_goods g ON og.goods_id=g.goods_id')
            ->join('yang_goods_attr ga ON og.attr_id=ga.goods_attr_id')
            ->join('yang_order_tui ot ON ot.order_id=o.order_id,ot.goods_id=og.goods_id')
            ->where(array('o.order_id'=>$order_id,'order_status' =>4))
            ->find();
            $this->assign('tui',1);
        }
        $this->assign('list',$list);
        $this->assign('goods',$goods);
        $this->display();
	}


}